// miniprogram/pages/ks/specialpractice/questions/questions.js

import {getDetailById} from "../../../../data/specailpretice"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    questions:[],
    currentQuestion:null,
    index:1,
    total:10
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.audioCtx.stop();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.audioCtx.stop();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    // let detail = getDetailById(options.id);
    // console.log(detail,'detail');
    this.audioCtx = wx.createInnerAudioContext();
    this.loadData(options.id);
  },
  onReady(){
    // this.audioCtx = wx.createAudioContext('myAudio');
  },
  loadData(id){
    let detail = getDetailById(id);
    let questions = detail.questions;
    this.audioCtx.src = questions[0].source;
    this.setData({
      name:detail.name,
      questions,
      currentQuestion:questions[0],
      total:questions.length,
      index:0
    })
  },
  play(){
    this.audioCtx.play()
  },
  down(){
    let {index,questions} = this.data;
    this.audioCtx.stop();
    index++;
    let currentQuestion = questions[index];
    this.audioCtx.src = currentQuestion.source;
    this.setData({
      currentQuestion,
      index
    });
  },
  complate(){
    this.audioCtx.stop();
    wx.navigateBack({
      delta: 0,
    })
  },
  showAnswer(){
    let {currentQuestion} = this.data;
    currentQuestion.showAnswer = true;
    this.setData({currentQuestion});
  }
})